/*
 * BtnSimpan.java
 *
 * Created on June 23, 2007, 4:00 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package latihanibatis.ui.event;

import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import latihanibatis.dao.MahasiswaDao;
import latihanibatis.domain.Mahasiswa;
import latihanibatis.ui.MhsTabelModel;
import latihanibatis.ui.PanelStatus;

/**
 *
 * @author javamaniac
 */
public class BtnSimpan extends AbstractAction {
    
    private final javax.swing.JTextField txtAlamat;
    private final javax.swing.JTextField txtKeterangan;
    private final javax.swing.JTextField txtKota;
    private final javax.swing.JFormattedTextField txtNIM;
    private final javax.swing.JTextField txtNama;
    private final PanelStatus status;
    private final MhsTabelModel tableModel;
    private final JButton btnCari;
    private final JButton btnSimpan;
    private final JButton btnEdit;
    private final JButton btnDelete;
    private final MahasiswaDao mhsDao;
    
    /** Creates a new instance of BtnSimpan */
    public BtnSimpan(JFormattedTextField txt1, JTextField txt2, JTextField txt3,
        JTextField txt4, JTextField txt5, PanelStatus stat, MhsTabelModel tblMdl,
        JButton btnCr, JButton btnSmp, JButton btnEdit, JButton btnDel, 
        MahasiswaDao dao) {
        
        super();
        this.txtNIM         = txt1;
        this.txtNama        = txt2;
        this.txtKota        = txt3;
        this.txtAlamat      = txt4;
        this.txtKeterangan  = txt5;
        this.status         = stat;
        this.tableModel     = tblMdl;
        this.btnCari        = btnCr;
        this.btnSimpan      = btnSmp;
        this.btnEdit        = btnEdit;
        this.btnDelete      = btnDel;
        this.mhsDao         = dao;
    }
    
    public void actionPerformed(final ActionEvent e) {
        Mahasiswa mhsBaru = new Mahasiswa();
        
        final int intNim = ((Number)txtNIM.getValue()).intValue();
        final String nama = txtNama.getText().toUpperCase();
        final String kota = txtKota.getText().toUpperCase();
        final String alamat = txtAlamat.getText().toUpperCase();
        final String keter  = txtKeterangan.getText().toUpperCase();
        
        try {
            if (intNim <= 0 || nama.trim().length() <= 0 ||
                kota.trim().length() <= 0 || alamat.trim().length() <= 0) {
                status.setStatus("Maap Kolom Isian Bertanda * Tidak Boleh Kosong !!", "ERROR");
            } else {
                mhsBaru.setNim(intNim);
                mhsBaru.setNama(nama);
                mhsBaru.setKota(kota);
                mhsBaru.setAlamat(alamat);
                mhsBaru.setKeterangan(keter);
                
                // Save it to database
                mhsDao.insertMhs(mhsBaru);
                
                // if done, give message to user and add into table.
                tableModel.addMhs(intNim, nama, kota, alamat, keter);
                
                // Clearing all textField
                txtNIM.setValue(0);
                txtNama.setText("");
                txtKota.setText("");
                txtAlamat.setText("");
                txtKeterangan.setText("");
                
                status.setStatus("Data Mahasiswa Berhasil Disimpan !!", "SUKSES");
                JOptionPane.showMessageDialog(null, "Data Mhs Tersimpan !!");
            }
        } catch (Exception ecpt) {
            status.setStatus("Data Mahasiswa Gagal Disimpan !!", "ERROR");
        }
    }
}
